package gui;

import gui.addrecipe.AddRecipe;
import gui.categories.CategoryPanel;
import gui.overview.OverviewPanel;
import gui.search.SearchPanel;

import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;

import org.jdom.JDOMException;

import start.Start;
import xml.XMLCategoryReader;
import xml.XMLRecipeReader;

import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

/**
 * Diese Klasse enthaelt das Tabmenue und laedt die verschiedenen Ansichten
 * (Uebersicht, Hinzufuegen, Suchen und Kategorien)
 * @author Dominik Korittki, Dennis Wittkoetter, Dennis Oeguetveren
 * 
 */
public class RootPanel extends JPanel {
	/**ID zur Serialisierung*/
	private static final long serialVersionUID = 1L;
	/**Tabs zur Navigation*/
	final private JTabbedPane tabbedPane = new JTabbedPane();
	/**XML-Reader fuer Kategorien*/
	private static XMLCategoryReader xmlCategoryReader;
	/**XML-Reader fuer Rezepte*/
	private static XMLRecipeReader xmlRecipeReader;
	/**Panel fuer Rezeptuebersicht*/
	private OverviewPanel panel1;
	/**Panel fuer das Hinzufuegen von Rezepten*/
	private AddRecipe panel2;
	/**Panel fuer Suche*/
	private SearchPanel panel3;
	/**Panel fuer Kategorieverwaltung*/
	private CategoryPanel panel4;
	
	/**
	 * Hinzufuegen der Panels zur Tabliste
	 * @throws JDOMException 
	 * @throws IOException 
	 */
	public RootPanel() throws IOException, JDOMException {
		super(new GridLayout(1, 1));	//JPanel-Konstruktor aufrufen
		
		xmlCategoryReader = new XMLCategoryReader(Start.getKategoriexml());
		xmlRecipeReader = new XMLRecipeReader(Start.getRezeptexml());
		
		tabbedPane.addMouseListener(new MouseListener() {
			@Override public void mouseClicked(MouseEvent e) {
				try {
					OverviewPanel.getTable().updateFromXML(xmlRecipeReader);
					CategoryPanel.getTable().updateFromXML(xmlCategoryReader);
				} catch (JDOMException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			@Override public void mouseEntered(MouseEvent e) {}
			@Override public void mouseExited(MouseEvent e) {}
			@Override public void mousePressed(MouseEvent e) {}
			@Override public void mouseReleased(MouseEvent e) {}
		});
		
		panel1 = new OverviewPanel();
        tabbedPane.addTab("Übersicht", new ImageIcon(Start.getImgs().getEditSmall()), panel1);
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        panel2 = new AddRecipe();
        tabbedPane.addTab("Rezept hinzufügen", new ImageIcon(Start.getImgs().getPlusSmall()), panel2);
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
         
        panel3 = new SearchPanel();
        tabbedPane.addTab("Suche", new ImageIcon(Start.getImgs().getSearchSmall()), panel3);
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
         
        panel4 = new CategoryPanel();
        tabbedPane.addTab("Kategorien verwalten", new ImageIcon(Start.getImgs().getKategorienSmall()), panel4);
        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
         
        //Add the tabbed pane to this panel.
        add(tabbedPane);
         
        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
	}

	/**
	 * Liefert das Panel der Tabs
	 * @return	Tab-Panel
	 */
	public JTabbedPane getTabbedPane() {
		return tabbedPane;
	}
	/**
	 * Liefert das Panel der Rezeptuebersicht
	 * @return	Uebersichtspanel
	 */
	public OverviewPanel getPanel1() {
		return panel1;
	}

	/**
	 * Liefert das Panel zum Erzeugen neuer Rezepte
	 * @return	Rezept-anlegen-Panel
	 */
	public AddRecipe getPanel2() {
		return panel2;
	}

	/**
	 * Liefert das Panel fuer die Suche
	 * @return	Suchen-Panel
	 */
	public SearchPanel getPanel3() {
		return panel3;
	}

	/**
	 * Liefert das Panel fuer die Kategorieverwaltung
	 * @return	Kategorieverwaltungspanel
	 */
	public CategoryPanel getPanel4() {
		return panel4;
	}
	
	
}
